package mscs.client.member.logic;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import mscs.data.IFileDataStore;
import mscs.data.IMemberProxy;
import mscs.data.pojo.Member;

public class AddMemberUtil implements IAddMemberUtil{
	private ArrayList<Member> memberList;  		//已有会员列表
	private ArrayList<Member> newList = new ArrayList<Member>(); 	//新会员列表
	private IFileDataStore fds = new IMemberProxy();
	
	public AddMemberUtil(){
		memberList = (ArrayList<Member>)fds.getObjects();
	}
	
	public String generateID() {
		Random generator = new Random();
		int newID = generator.nextInt(9999);
		if(newID < 9000){
			return String.valueOf(1000+newID);
		}else{
			return String.valueOf(newID);
		}
	}
	/**
	 * 判断newID在memberList中是否已被用过
	 * @param newID
	 * @return 
	 */
	private boolean isFound(String newID){
		for(Member member: memberList){
			if(member.getId().equals(String.valueOf(newID)))
				return false;
		}
		return true;
	}

	public void addToNewList(Member member) {
		this.newList.add(member);
	}

	public Member getByIndex(int index) {
		return this.newList.get(index);
	}

	public void setByIndex(int index, Member member) {
		this.newList.set(index, member);
		
	}

	public boolean commitNewList() {
		if(newList.size() > 0){
			fds.add(newList);
                                                      newList = new ArrayList<Member>();
			return true;
		}
		return false;
	}

	@Override
	public void removeFromNewList(int index) {
		newList.remove(index);
		
	}

	@Override
	public List<Member> getNewList() {
		return newList;
	}
	
	

}
